#include "MainFrame.h"
#include "wx/filedlg.h"
#include "wx/utils.h"

wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
    EVT_MENU(wxID_OPEN, MainFrame::OnOpenFile)
    EVT_MENU(wxID_EXIT, MainFrame::OnExit)
    EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
wxEND_EVENT_TABLE()

MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size, long style) : wxFrame(NULL,
            wxID_ANY, title, pos, size, style)
{
    createImageView();
    wxMenuBar *menuBar = createMenus();
    SetMenuBar(menuBar);
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
}

MainFrame::~MainFrame()
{

}
void MainFrame::OnExit(wxCommandEvent &event)
{
    Close(true);
}
void MainFrame::OnAbout(wxCommandEvent &event)
{
    wxMessageBox("This is a wxWidgets' Hello world sample", "About Hello World", wxOK | wxICON_INFORMATION);
}
void MainFrame::OnOpenFile(wxCommandEvent &event)
{
    //获取当前用户的目录
    wxString userHomePath = wxGetHomeDir();
    wxString caption = wxT("选择文件");
    wxString wildcard = wxT("图片文件(*.bmp;*.jpg;*.png)|*.bmp;*.jpg;*.png");
    wxString defaultFilename = wxEmptyString;
    wxFileDialog dialog(this, caption, userHomePath, defaultFilename, wildcard, wxFD_OPEN);
    if(dialog.ShowModal() == wxID_OK)
    {
        wxString path = dialog.GetPath();
        int filterIndex = dialog.GetFilterIndex();
    }
}

//创建菜单
wxMenuBar *MainFrame::createMenus()
{
    wxMenu *menuFile = new wxMenu;
    wxString openFile(wxT("&打开图片"));
    menuFile->Append(wxID_OPEN, openFile + "...\tCtrl-O", wxT("打开图片文件"));
    menuFile->AppendSeparator();
    wxString quit(wxT("&退出"));
    menuFile->Append(wxID_EXIT, quit + "...\tCtrl-Q", wxT("退出程序"));
    wxMenu *menuHelp = new wxMenu;
    wxString about(wxT("&关于"));
    menuHelp->Append(wxID_ABOUT, about);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, wxT("&文件"));
    menuBar->Append(menuHelp, wxT("&帮助"));
    return menuBar;
}

//创建图片显示窗口
void MainFrame::createImageView()
{
    this->SetSizeHints(wxDefaultSize, wxDefaultSize); // 设置对话框最小大小

    wxSize clientSize = GetClientSize();

    wxFlexGridSizer *fgSizer = new wxFlexGridSizer(1, 2, 2, 2);
    fgSizer->SetFlexibleDirection(wxBOTH);
    fgSizer->AddGrowableCol(0);
    fgSizer->AddGrowableRow(0);

    int windowSize = (int)(clientSize.GetWidth() * 0.75) - 10;
    m_scrolledWindow = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxSize(windowSize, clientSize.GetHeight()),
                                            wxHSCROLL | wxVSCROLL);
    m_scrolledWindow->SetScrollRate(5, 5);
    fgSizer->Add(m_scrolledWindow, 1, wxEXPAND | wxALL, 1);

    int panelSize = clientSize.GetWidth() - windowSize;
    m_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(panelSize, clientSize.GetHeight()), wxTAB_TRAVERSAL);
    wxColour color(255, 0, 0);
    m_panel->SetBackgroundColour(color);

    fgSizer->Add(m_panel, 1, wxEXPAND | wxALL, 1);

    this->SetSizer(fgSizer); // 绑定对话框和布局控件
    this->Layout();

    this->Centre(wxBOTH);
}